; docformat = 'rst'
;
;+
;
; :Purpose:
;   Specifies the fraction of each specified country that falls within each 'aggregated' region within local regions.
;   This should be defined before the metropolis algorithm is run, so that national total emissions can be estimated
;   in the Metropolis algorithm, taking into account covariance between regions
;   
; :Inputs:
;   sim_name: (string) simulation name
;   
; :Requires:
;   Countries must be specified in {sim_name}.params in the COUNTRY_TRACK variable.  
;   
;   Country names MUST match the strings in mr_country_info.sav from mr-idl-library, or they won't be detected.
;   To find country names do:: 
;     restore, '{path}/mr_country_info.sav'
;     print, country_name
;   
;   to see full list of country names
;   
; :Outputs:
;   Generates {input_directory}/{sim_name}/country_track.sav, which contains the string variable *country_track*
;   and the floating point fraction of each country accounted for by each element of the state vector (x) in
;   *country_track_fraction*
;   
; :History:
; 	Written by: Matt Rigby, MIT, Aug 19, 2011
;
;-
pro cels_countries_to_track, sim_name

  country_track=cels_get_parameter(sim_name, 'COUNTRY_TRACK')
  lon_domain=cels_get_parameter(sim_name, 'LON_DOMAIN')
  StartY=cels_get_parameter(sim_name, 'STARTY')
  EndY=cels_get_parameter(sim_name, 'ENDY')
  
  country_track=StrUpCase(country_track[sort(country_track)])

  nCountries=n_elements(country_track)

  country_track_fraction=!null
  
  N_LR=n_elements(lon_domain[0, *])

  for yi=StartY, EndY-1 do begin
    for lri=0, n_LR-1 do begin
      restore, cels_filestr(/Input, sim_name + '/ar_' + string(lri, format='(I02)') + '.sav')
      restore, cels_filestr(/Input, sim_name + '/emissions/lr_' + string(lri, format='(I02)') + '_' + string(yi) + '.sav')
  
      statesize_lr=n_elements(ar_wh->keys())
  
      country=find_country(lonq, latq, /ocean, name=name)
  
      country_fraction_lr=fltarr(stateSize_lr, nCountries)
  
      for xi=0, stateSize_lr-1 do begin
        country_temp=country[ar_wh[xi]]
        q_temp=q[ar_wh[xi]]
        country_uniq=country_temp[uniq(country_temp, sort(country_temp))]
        
        for ci=0, n_elements(country_uniq)-1 do begin
          country_wh=where(country_track eq name[country_uniq[ci]], count)
          if count ne 0 then begin
            Country_fraction_lr[xi, country_wh]=total(q_temp[where(country_temp eq country_uniq[ci])])/total(q_temp)
          endif
        endfor
      endfor
  
      country_track_fraction=[country_track_fraction, country_fraction_lr]
  
    endfor
  endfor
  ;Check dimensions
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  restore, cels_filestr(/Input, sim_name + '/sensitivity.sav')
  restore, cels_filestr(/Input, sim_name + '/state.sav')
  
  if n_elements(country_track_fraction[*, 0]) ne n_elements(x_ap) - N_IC - N_NLRT then stop
  
  x_country_track_fraction=fltarr(n_elements(x_ap), n_elements(country_track))
  x_country_track_fraction[n_IC + n_NLRT:-1, *]=country_track_fraction

  country_track_fraction=x_country_track_fraction

  save, filename=cels_filestr(/Input, sim_name + '/country_track.sav'), country_track, country_track_fraction

end